ARG PHP_VERSION
FROM php:${PHP_VERSION}-fpm-alpine

ARG TIMEZONE
RUN apk update && apk add -U tzdata \ 
    && cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \ 
    && rm -rf /var/cache/apk/*

RUN apk add --no-cache libjpeg-turbo libjpeg-turbo-dev libpng libpng-dev freetype freetype-dev openssl-dev $PHPIZE_DEPS 

RUN docker-php-ext-configure gd \
    # --enable-gd-native-ttf \
    --with-jpeg-dir=/usr/include/ \
    --with-freetype-dir=/usr/include/freetype2 \
    && docker-php-ext-install -j$(nproc) gd mysqli pdo_mysql sockets 

# memcached
ARG INSTALL_MEMCACHED=false
RUN if [ ${INSTALL_MEMCACHED} = true ]; then \ 
    apk add --no-cache libmemcached-dev \
    && pecl install memcached \
    && docker-php-ext-enable memcached \
    && pecl clear-cache \ 
;fi

#redis
ARG INSTALL_REDIS=false
RUN if [ ${INSTALL_REDIS} = true ]; then \ 
    pecl install redis \
    && docker-php-ext-enable redis \
    && pecl clear-cache \ 
;fi

# #mongodb
ARG INSTALL_MONGODB=false
RUN if [ ${INSTALL_MONGODB} = true ]; then \
    pecl install mongodb \
    && docker-php-ext-enable mongodb \
    && pecl clear-cache \
;fi

# #imagick
ARG INSTALL_IMAGICK=false
RUN if [ ${INSTALL_IMAGICK} = true ]; then \
    apk add --no-cache imagemagick-dev \ 
    && pecl install imagick \
    && docker-php-ext-enable imagick \ 
    && pecl clear-cache \
;fi

# install xdebug and config phpstom xdebug
ARG INSTALL_XDEBUG=false
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
    pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.idekey = PHPSTORM" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_enable = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back = on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_autostart = off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_port = 9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && pecl clear-cache \ 
;fi
	
# install swoole extention
ARG INSTALL_SWOOLE=false
ARG SWOOLE_VERSION
RUN if [ ${INSTALL_SWOOLE} = true ]; then \
    wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-async-redis --enable-mysqlnd --enable-sockets --enable-openssl \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole \ 
;fi

# install composer
ARG INSTALL_COMPOSER=false
RUN if [ ${INSTALL_COMPOSER} = true ]; then \
    curl -sS https://install.phpcomposer.com/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && composer config -g repositories.packagist composer https://packagist.phpcomposer.com \
    && composer self-update --clean-backups \
;fi


# install mssql
ARG INSTALL_MSSQL=false
ARG MSSQL_VERSION=17.5.2.1-1
ENV MSSQL_VERSION=${MSSQL_VERSION}
RUN if [ ${INSTALL_MSSQL} = true ]; then \
    set -xe \
    && apk add --no-cache --virtual .persistent-deps \
        freetds \
        unixodbc \
    && apk add --no-cache --virtual .build-deps \
        unixodbc-dev \
        freetds-dev \
    && apk add --no-cache gnupg --virtual .build-dependencies -- && \
    # Adding custom MS repository for mssql-tools and msodbcsql
    curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_${MSSQL_VERSION}_amd64.apk && \
    curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_${MSSQL_VERSION}_amd64.apk && \
    # Verifying signature
    curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_${MSSQL_VERSION}_amd64.sig && \
    curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_${MSSQL_VERSION}_amd64.sig && \
    # Importing gpg key
    curl https://packages.microsoft.com/keys/microsoft.asc  | gpg --import - && \
    gpg --verify msodbcsql17_${MSSQL_VERSION}_amd64.sig msodbcsql17_${MSSQL_VERSION}_amd64.apk && \
    gpg --verify mssql-tools_${MSSQL_VERSION}_amd64.sig mssql-tools_${MSSQL_VERSION}_amd64.apk && \
    # Installing packages
    echo y | apk add --allow-untrusted msodbcsql17_${MSSQL_VERSION}_amd64.apk mssql-tools_${MSSQL_VERSION}_amd64.apk && \
    # Deleting packages
    apk del .build-dependencies && rm -f msodbcsql*.sig msodbcsql*.apk mssql-tools*.sig mssql-tools*.apk \ 
    && export PATH=$PATH:/opt/mssql-tools/bin \
    && pecl install sqlsrv pdo_sqlsrv && \
    docker-php-ext-enable sqlsrv pdo_sqlsrv  \
;fi

EXPOSE 9000
CMD ["php-fpm"]